package com.tanhua.server.controller;

import com.tanhua.model.vo.MyPageResultVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class SettingsController {

    @Autowired
    private SettingsService settingsService;


    /**
     * 用户通用设置 - 读取
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {

        SettingsVo settingsVo = settingsService.settings();

        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题 - 保存
     *
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        String txt = (String) map.get("content");

        settingsService.question(txt);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置 - 保存
     *
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notificationsSetting(@RequestBody Map map) {

        settingsService.notificationsSetting(map);

        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单分页查询(自己实现思路)
     * @RequestParam: 接收表单参数,可以设置默认值
     * @param page
     * @param pagesize
     * @return
     */
    //@GetMapping("/blacklist")
    public ResponseEntity blackList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize) {
        log.info("page: {},pageSize: {}", page, pagesize);

        MyPageResultVo myPageResultVo = settingsService.blackList(page, pagesize);

        return ResponseEntity.ok(myPageResultVo);
    }

    /**
     * 黑名单分页查询(课本实现思路)
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blackList2(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize){

        PageResult pageResult = settingsService.blackList2(page,pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     *
     * @param uid
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeBlackUser(@PathVariable Long uid) {

        settingsService.removeBlackUser(uid);

        return ResponseEntity.ok(null);
    }
}
